import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5

import QtQuick.VirtualKeyboard 2.2
import QtQuick.VirtualKeyboard.Settings 2.2

import QtQuick.Controls.Styles 1.2

SystemWindow{
    id: textWindow
    //visible: true
    width: Screen.desktopAvailableWidth
    height: Screen.desktopAvailableHeight
    title: qsTr("文本编辑器")

    MouseArea {
        anchors.fill: parent;
        onPressed:{
             mouse.accepted = true
        }
    }

    Button {
        id: exitFile
        text: "退出"
        //anchors.left: parent.left
        anchors.right: parent.right
        //anchors.leftMargin: 8
        anchors.rightMargin: 8
        //anchors.bottom: parent.bottom
        anchors.top: parent.top
        anchors.topMargin:8
        //anchors.bottomMargin: 8

        //按下按钮，打开文件对话框
     //   onClicked: textWindow.close()

        MouseArea{
            anchors.fill: parent;
            onClicked:{
                console.log("textWindow close:")
                textWindow.close()
            }
          //  z: 4
        }
    }



    Flickable {
        id: flick

        width:Screen.desktopAvailableWidth/2
        height:Screen.desktopAvailableHeight/2
        contentWidth: edit.paintedWidth
        contentHeight: edit.paintedHeight
        clip: true

        function ensureVisible(r)
        {
            if (contentX >= r.x)
                contentX = r.x;
            else if (contentX+width <= r.x+r.width)
                contentX = r.x+r.width-width;
            if (contentY >= r.y)
                contentY = r.y;
            else if (contentY+height <= r.y+r.height)
                contentY = r.y+r.height-height;
        }

        TextEdit {
            id: edit
            width: flick.width
            height: flick.height
            focus: true
            wrapMode: TextEdit.Wrap
            onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
        }
    }
    InputPanel
    {
        id: inputPannelID
        z: 99
        y: textWindow.height     // 默认让其处于窗口最下方,貌似隐藏一样
        width: textWindow.width/1.5
        anchors.horizontalCenter: parent.horizontalCenter
        // anchors.bottomMargin: y/1080
        //visible: true       // 一直显示

        states: State
        {
            name: "visible"
            when: inputPannelID.active
            PropertyChanges
            {
                target: inputPannelID
                y: textWindow.height-inputPannelID.height
            }
        }
        transitions: Transition
        {
            from: ""
            to: "visible"
            reversible: true
            ParallelAnimation
            {
                NumberAnimation
                {
                    properties: "y"
                    duration: 250
                    easing.type: Easing.InOutQuad
                }
            }
        }

        Component.onCompleted:
        {
            // VirtualKeyboardSettings.styleName = "retro"                         // 复古样式
            VirtualKeyboardSettings.wordCandidateList.alwaysVisible = true
            VirtualKeyboardSettings.activeLocales = ["en_US","zh_CN","ja_JP"]   // 英语、中文、日语 (若不设置,则语言就有很多种)
        }
    }
}
